package cloud.zhixinglu.www.spring.boot.mybatis.flex.generater;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.BaseMapper;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Lombok;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * Codegen
 * </p>
 *
 * @author weixin
 * @date 2023/8/11 18:30
 */
public class Codegen {

    /**
     * 代码生成者
     */
    private static final String AUTHOR = "WeiXin";

    /**
     * 自，日期（yyyy-MM-dd）
     */
    private static final String SINCE = getDateFormat();


    /**
     * 根路径
     */
    private static final String ROOT_PATH = System.getProperty("user.dir") + "/database-orm";

    /**
     * 项目名称
     *
     */
    private static final String PROJECT_NAME = "spring-boot-mybatis-flex";

    /**
     * 包名
     */
    private static final String PACKAGE_NAME = "cloud.zhixinglu.www.spring.boot.mybatis.flex";


    /**
     * 输出目录
     *
     */
    private static final String SOURCE_DIR = stitchingPath(ROOT_PATH, PROJECT_NAME, "/src/main/java/");

    /**
     * Mapper 输出目录
     *
     */
    private static final String XML_PATH = stitchingPath(ROOT_PATH , PROJECT_NAME,"/src/main/resources/mapper/");

    /**
     * 获取当前格式化时间 yyyy-MM-dd
     * @return String
     */
    private static String getDateFormat(){
        LocalDate currentDate = LocalDate.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);

        System.out.println("Current Date: " + formattedDate);
        return formattedDate;
    }

    /**
     * 拼接路径
     * @param rootPath String 根路径
     * @param projectName String 项目名
     * @param subPath String 剩余子目录路径
     * @return String
     */
    public static String stitchingPath(String rootPath, String projectName, String subPath) {

        Path fullPath = Paths.get(rootPath, projectName, subPath);

        return fullPath.toString();
    }



    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:postgresql://39.105.36.172:5432/cloud_agi_tool?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&stringtype=unspecified");
        dataSource.setUsername("root");
        dataSource.setPassword("zjwz8888");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 注释配置
        globalConfig.getJavadocConfig()
                .setAuthor(AUTHOR)
                .setSince(SINCE);

        // 设置根包
        globalConfig.getPackageConfig()
                .setSourceDir(SOURCE_DIR)
                .setBasePackage(PACKAGE_NAME)
                .setMapperXmlPath(XML_PATH);

        // 设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setGenerateSchema("public")
                .setGenerateTable("test");

        // 控制器配置
        globalConfig.enableController()
                // Controller 类的后缀
                .setClassSuffix("Controller")
                // 是否覆盖之前生成的文件
                .setOverwriteEnable(true)
                // REST 风格的 Controller
                .setRestStyle(true);

        // 设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                // Entity 是否使用 Lombok 注解
                .setWithLombok(true)
                // Entity 类的后缀
                .setClassSuffix("Entity")
                // 是否覆盖之前生成的文件
                .setOverwriteEnable(true);

        // 设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.getMapperConfig()
                .setOverwriteEnable(true);

        // 设置生成 Service
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.getServiceConfig().setOverwriteEnable(true);

        // 设置生成 ServiceImpl
        globalConfig.setServiceImplGenerateEnable(true);
        globalConfig.getServiceImplConfig()
                .setOverwriteEnable(true);

        // 设置生成 Mapper XML
        globalConfig.setMapperXmlGenerateEnable(true);
        globalConfig.enableMapperXml()
                .setFileSuffix("Mapper")
                .setOverwriteEnable(true);

        // 可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.setColumnConfig("algorithm_statistics", columnConfig);

        return globalConfig;
    }
}
